import { TaskBase } from '../TaskBase';

export class TaskWithdraw extends TaskBase {
    shouldRun(creep: Creep) {
        const d = this.taskData;
        return 'store' in this.target &&
            this.target.store[d.rType] >= (d.amount ?? 1);
    }
    _run(creep: Creep) {
        const d = this.taskData;
        const target = <AnyStoreStructure>this.target;
        const result = creep.withdraw(target, d.rType, d.amount);
        if (result === OK) {
            return OK;
        }
        else if (result === ERR_NOT_IN_RANGE) {
            creep.goTo(target);
            return ERR_NOT_IN_RANGE;
        }
        else {
            return OK;
        }
    }
}